import React from 'react';
import { useRequest } from '@/hooks';
import { message } from 'antd';
import { getChannelListWithTalent } from './Channel/service';

const def = [];
export default function useChannelDict(necessary) {
    const { data: channelDict, loading: channelDictLoading, run } = useRequest(getChannelListWithTalent, {
        initialData: def,
        manual: necessary,
        formatResult: res => {
            if (res) {
                const { code, data, msg } = res;
                if (code === 10000) {
                    if (data && data.length) {
                        return data.map(item => ({
                            ...item,
                            text: item.channelName,
                            value: item.channelCode
                        }));
                    }
                    return def;
                }
                message.error(msg);
            }
            return def;
        }
    });
    React.useEffect(() => {
        if (necessary) {
            run();
        }
    }, [necessary]);
    return { channelDict, channelDictLoading };
}
